package com.user.notiy.pool;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.plat.common.UmsConfig;
import com.ums.umsmsg.UmsMsg;
import com.ums.umsmsg.proc.Trans;
import com.user.notiy.UmsStringUtils;
import com.user.notiy.common.utils.NoticeList;
import com.user.notiy.dao.TNoticeMapper;
import com.user.notiy.entity.TNotice;

public abstract class NoticeThread extends Thread {
	private static Logger log = LoggerFactory.getLogger(NoticeThreadPool.class);
	private static final long SLEEP_TIME = 1000;
	private static final String CFG_FILE = "Notice.xml";
	private static final String CFG_DIR = UmsConfig.getProperty("msg", "cfg.dir");
	
	@Autowired
	protected TNoticeMapper tNoticeDAO;

	protected Trans trans;
	protected String typeID;
	protected String groupID;
	
	protected abstract String getURL(Map<String, String> map);
	protected abstract byte[] send(String url, byte[] reqMsg);
	
	/**
	 * @Func	NoticeThread
	 * @Brief	构造函数
	 */
	public NoticeThread(String name, String typeID, String groupID, TNoticeMapper tNoticeDAO) {
		super(name);

		trans = UmsMsg.recognTrans(CFG_DIR + CFG_FILE, typeID.getBytes());
		this.tNoticeDAO = tNoticeDAO;
		this.typeID = typeID;
		this.groupID = groupID;
		
		this.setDaemon(true);
		NoticeList.createList(typeID, groupID);
	}

	/**
	 * @Func	run
	 * @Brief	运行
	 */
	@Override
	public void run() {
		while(true) {
			/* 获取通知字典 */
			Map<String, String> map = NoticeList.next(typeID, groupID);

			try {
				if(map != null && !map.isEmpty()) {
					log.info("[" + this.getName() + "]发现需要通知的消息，消息内容：" + map);
					
					/* 映射 */
					Map<String, String> newMap = mapp(map);
					if(newMap == null || newMap.size() <= 0) {
						update(map.get("NoticeID"), false);
						continue;
					}
					
					/* 组包 */
					byte[] reqMsg = pack(newMap);
					if(reqMsg == null || reqMsg.length == 0) {
						update(map.get("NoticeID"), false);
						continue;
					}
					/* 发送 */
					byte[] rspMsg = send(getURL(map), reqMsg);
					if(rspMsg == null || rspMsg.length == 0) {
						update(map.get("NoticeID"), false);
						continue;
					}
					/* 解包 */
					Map<String, String> tmpMap = unpack(rspMsg);
					if(tmpMap == null) {
						update(map.get("NoticeID"), false);
						continue;
					} else {
						map.putAll(tmpMap);
					}
					/* 更新数据库 */
					update(map.get("NoticeID"), true);
				} else {
					//log.info("[" + this.getName() + "]未发现需要通知的消息，休息一下吧...");
					Thread.sleep(SLEEP_TIME);
				}
			} catch (Exception e) {
				e.printStackTrace();
				update(map.get("NoticeID"), false);
				continue;
			}
		}
	}
	

	/**
	 * @Func	pack
	 * @Brief	组包
	 */
	protected byte[] pack(Map<String, String> map) {
		return UmsMsg.pack(CFG_DIR + trans.getCFG(Trans.IN), map);
	}

	/**
	 * @Func	unpack
	 * @Brief	解包
	 */
	@SuppressWarnings("unchecked")
	protected Map<String, String> unpack(byte[] rspMsg) {
		return UmsMsg.unpack(CFG_DIR + trans.getCFG(Trans.IN), rspMsg);
	}
	
	/**
	 * @Func	mapp
	 * @Brief	映射
	 */
	protected Map<String, String> mapp(Map<String, String> map) {
		Map<String, String> newMap = new HashMap<String, String>();
		UmsMsg.mapping(CFG_DIR + trans.getCFG("MAPP"), map, newMap);
		return newMap;
	}
	
	/**
	 * @Func	update
	 * @Brief	更新
	 */
	protected void update(String noticeID, boolean isSucc) {
		if(UmsStringUtils.isExists(noticeID)) {
			TNotice notice = new TNotice();
			
			notice.setNoticeID(noticeID);
			if(isSucc) {
				notice.setNoticeState(TNotice.NOTIFY_SUCC);
			} else {
				notice.setNoticeState(TNotice.NOTIFY_FAIL);
			}
			
			tNoticeDAO.save(notice);
		}
	}
}
